package source;
import java.rmi.*;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.security.Policy;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.HashSet;
import GUI.*;

import dataobjects.*;

public class Client extends UnicastRemoteObject implements Serializable
{
	private static final long serialVersionUID = 11;
	public enum frame {LoginFrame, ClientFrame,};	
	ServerInterface server = null;
	User clientuser = null;
	
	protected Client() throws RemoteException, NotBoundException
	{
		super();
		Registry reg = LocateRegistry.getRegistry("localhost", 1099);
		server = (ServerInterface)reg.lookup("KinoServer");				
	}
	
	public static void main(String[] args) throws Exception
	{
			Client tempclient = new Client();
			tempclient.callNextFrame(Client.frame.LoginFrame);	
	} 
	
	public boolean login(String username, char[] password) // WICHTIGE METHODE 
	{
		boolean returnvalue = false;
		if(server != null && username != null && password != null && password.length > 0)
		{
			try
			{
				clientuser = server.checkLoginData(username, new String(password));
				if(clientuser != null)
				{
					returnvalue = true;
				}
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
		System.out.println("returnvalue" + returnvalue);
		return returnvalue;
	}
	
	public void logout()	// WICHTIGE METHODE 
	{
		try
		{
			System.out.println("Client.logout -->");
			server.logout(clientuser);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			System.out.println("Client.logout <--");
		}
	}

	public User getClientuser() {	// WICHTIGE METHODE 
		return clientuser;
	}

	public void setClientuser(User clientuser) {
		this.clientuser = clientuser;
	}
	
	public void callNextFrame(Client.frame frame)	// WICHTIGE METHODE 
	{		
		try
		{
			if(Client.frame.LoginFrame.equals(frame))
			{
				LoginFrame loginframe = new LoginFrame(this);
			}
			else if(Client.frame.ClientFrame.equals(frame))
			{
				if(this.clientuser != null)
				{
					ClientFrame clientframe = new ClientFrame(this);
				}
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public ArrayList<Film> getFilmList() throws Exception {
		return server.getFilmList();
	}
	
	public ArrayList<Vorstellung >getVorstellungList(Film film)throws Exception {
		return server.getVorstellungList(film);
	}
	
	public void insertNewFilm(String filmname, String filmbeschreibung, Time filmdauer, String filmtrailer)throws Exception
	{
		server.insertNewFilm(filmname, filmbeschreibung, filmdauer, filmtrailer);		
	}
	
	public void editFilm(int filmid, String filmname, String filmbeschreibung, Time filmdauer, String filmtrailer)throws Exception
	{
		server.editFilm(filmid, filmname, filmbeschreibung, filmdauer, filmtrailer);		
	}
	
	public void deleteFilm(int filmid)throws Exception
	{
		server.deleteFilm(filmid);		
	}
	
	public void insertNewShow(int saalid, int filmid, Date date, Time beginn)throws Exception
	{
		server.insertNewShow(saalid, filmid, date, beginn);		
	}
	
	public void editShow(int vid, int saalid, int filmid, Date date, Time beginn)throws Exception
	{
		server.editShow(vid, saalid, filmid, date, beginn);		
	}
	
	public void deleteShow(int vid)throws Exception
	{
		server.deleteShow(vid);		
	}
	
	public boolean checkFreeSitzplatz(int v_id, int sitz_nr) throws Exception
	{
		return server.checkFreeSitzplatz(v_id, sitz_nr);
	}
	
	public void insertNewReservation(int v_id, int sitz_nr) throws Exception
	{
		server.insertNewReservation(v_id, sitz_nr);
	}
	
	public ArrayList<Integer> getReservationList(int v_id) throws Exception
	{
		return server.getReservationList(v_id);
	}
}
